<template>
  <el-card>
    <div slot="header" class="">
      <span>{{ popData.title }}</span>
    </div>
      <el-form :model="form" :rules="rules" ref="form" label-width="90px" class="form" label-position="right">
      <el-form-item label=" 公司" prop="companyId">
        <el-select
          v-model="form.companyId"
          class="inputWidth"
          clearable
          filterable
          placeholder="请选择公司"
        >
          <el-option
            v-for="item in companyList"
            :key="item.id"
            :label="item.companyName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label=" 部门名称" prop="deptName">
        <el-input v-model="form.deptName" class="inputWidth" placeholder="请填写 部门名称" />
      </el-form-item>
      <el-form-item label=" 省市区" prop="regions">
        <el-cascader
          v-model="form.regions"
          class="inputWidth"
          :props="props"
          filterable
          clearable
        />
      </el-form-item>
      <el-form-item label=" 办公地址" prop="address">
        <el-input v-model="form.address" class="inputWidth" placeholder="请填写详细办公地址" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="edit">{{ loadingText }}</el-button>
        <el-button type="primary">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import companyDept from '@/api/companyDept'
import company from '@/api/company'
import provinceRegions from '@/api/provinceRegions'

export default {
  props: ['popData'],
  data() {
    return {
      form: {},
      loadingText: '保存',
      companyList: [],
      props: {
        lazy: true,
        lazyLoad(node, resolve) {
          provinceRegions.getByParentId({ parentId: node.value }).then(resp => {
            const nodes = resp.data.map(item => ({
              value: item.id,
              label: item.name,
              leaf: item.level > 1
            }))
            resolve(nodes)
          })
        }
      },
      rules: {
        companyId: [
          { required: true, message: '请选择公司', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请填写部门地址', trigger: 'blur' },
          { max: 50, message: '最多填写50个字符', trigger: 'blur' }
        ],
        deptName: [
          { required: true, message: '请选择部门名称', trigger: 'blur' },
          { min: 2, max: 20, message: '请填写 2 到 20 个字符', trigger: 'blur' }
        ],
        regions: [
          { required: true, message: '请选择部门区域', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    const id = this.popData.id
    if (id) {
      this.form.id = id
      this.loadingText = '编辑'
      this.getDetailById(id)
    } else {
      this.loadingText = '保存'
    }
    this.searchCompany()
  },
  methods: {
    getDetailById(id) {
      companyDept.getById(id).then(res => {
        this.form = res.data
        this.form.regions = this.form.provinceRegions.split(',').map(Number)
      })
    },
    edit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.provinceRegions = this.form.regions.join(',')
          companyDept.edit(this.form).then(res => {
            this.$message.success(res.message)
            this.$router.push('/companyDept/index')
          })
        }
      })
    },
    searchCompany() {
      company.searchCompany({ companyName: this.companyName }).then(resp => {
        this.companyList = resp.data
      })
    }
  }
}
</script>
<style>
  @import "../../styles/dialogForm.scss";
.form{
  width: 80%;
  margin: auto auto;
}
</style>
